package cn.stylefeng.guns.modular.wms.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.rms.entity.Company;
import cn.stylefeng.guns.modular.rms.entity.CompanyPosition;
import cn.stylefeng.guns.modular.rms.mapper.CompanyMapper;
import cn.stylefeng.guns.modular.rms.mapper.CompanyPositionMapper;
import cn.stylefeng.guns.modular.rms.model.params.CompanyParam;
import cn.stylefeng.guns.modular.rms.model.params.CompanyPositionParam;
import cn.stylefeng.guns.modular.rms.model.result.CompanyPositionResult;
import cn.stylefeng.guns.modular.ums.entity.ShareLog;
import cn.stylefeng.guns.modular.ums.mapper.MemberMapper;
import cn.stylefeng.guns.modular.ums.mapper.ShareLogMapper;
import cn.stylefeng.guns.modular.ums.model.params.ShareLogParam;
import cn.stylefeng.guns.modular.ums.model.result.MemberResult;
import cn.stylefeng.guns.modular.ums.model.result.ShareLogResult;
import cn.stylefeng.guns.modular.wms.entity.Browse;
import cn.stylefeng.guns.modular.wms.entity.Resume;
import cn.stylefeng.guns.modular.wms.mapper.*;
import cn.stylefeng.guns.modular.wms.model.params.BrowseParam;
import cn.stylefeng.guns.modular.wms.model.params.JobExpectParam;
import cn.stylefeng.guns.modular.wms.model.params.ResumeParam;
import cn.stylefeng.guns.modular.wms.model.params.WorkExpParam;
import cn.stylefeng.guns.modular.wms.model.result.BrowseResult;
import cn.stylefeng.guns.modular.wms.model.result.JobExpectResult;
import cn.stylefeng.guns.modular.wms.model.result.ResumeResult;
import cn.stylefeng.guns.modular.wms.model.result.WorkExpResult;
import  cn.stylefeng.guns.modular.wms.service.BrowseService;
import cn.stylefeng.guns.modular.wms.service.ResumeService;
import cn.stylefeng.guns.utils.StringUtil;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 浏览记录表 服务实现类
 * </p>
 *
 * @author yzb
 * @since 2021-03-23
 */
@Service
public class BrowseServiceImpl extends ServiceImpl<BrowseMapper, Browse> implements BrowseService {
    @Resource
    private CompanyPositionMapper companyPositionMapper;
    @Resource
    private ResumeMapper resumeMapper;
    @Resource
    private ResumeService resumeService;
    @Resource
    private MemberMapper memberMapper;
    @Resource
    private CompanyMapper companyMapper;
    @Resource
    private ShareLogMapper shareLogMapper;
    @Resource
    private WorkExpMapper workExpMapper;
    @Resource
    private JobExpectMapper jobExpectMapper;

    @Override
    public void add(BrowseParam param){
        Browse entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(BrowseParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(BrowseParam param){
        BrowseResult result=this.findBySpec(param);
        if (result!=null){
            param.setId(result.getId());
            Browse oldEntity = getOldEntity(param);
            Browse newEntity = getEntity(param);
            ToolUtil.copyProperties(newEntity, oldEntity);
            this.updateById(newEntity);
        }else{
            this.add(param);
        }
        //携带推广码，增加推广查阅记录
        if (StringUtil.isNotEmpty(param.getTcode())){
            ShareLogResult shareLogResult=shareLogMapper.findBySpec(new ShareLogParam().setDataId(param.getPositionId()).setTcode(param.getTcode()));
            if (shareLogResult!=null){
                shareLogResult.setNumber(shareLogResult.getNumber()+1);
                shareLogMapper.updateById(new ShareLog(shareLogResult.getId(),shareLogResult.getNumber()));
            }
        }
        //添加浏览次数
        if (param.getType()==0){
            CompanyPosition companyPosition =companyPositionMapper.selectById(param.getPositionId());
            companyPosition.setBrowseNumber(companyPosition.getBrowseNumber()+1);
            companyPositionMapper.updateById(companyPosition);
        }else{
            Resume resume=resumeMapper.selectById(param.getResumeId());
            resume.setBrowseNumber(resume.getBrowseNumber()+1);
            resumeMapper.updateById(resume);
        }
    }

    @Override
    public BrowseResult findBySpec(BrowseParam param){
        return baseMapper.findBySpec(param);
    }

    @Override
    public List<BrowseResult> findListBySpec(BrowseParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(BrowseParam param){
        Page pageContext = getPageContext();
        IPage<BrowseResult> page = this.baseMapper.customPageList(pageContext, param);
        page.getRecords().forEach(item -> {
            if (item.getPositionId()!=null){
                item.setPosition(companyPositionMapper.findBySpec(new CompanyPositionParam().setId(item.getPositionId())));
                //招聘者信息
                MemberResult memberResult=memberMapper.getMemberById(item.getComMemberId());
                item.setMemberAvatar(memberResult.getAvatar());
                item.setMemberName(memberResult.getName());
                item.setMemberPostName(memberResult.getPostName());
                //企业信息
                Company company=companyMapper.selectById(item.getCompanyId());
                item.setCompanyName(company.getName());
                item.setCompanyStaffSize(company.getStaffSize());
                item.setCompanyNature(company.getNature());
            }
            if (item.getResumeId()!=null){
                item.setResume(resumeMapper.findBySpec(new ResumeParam().setId(item.getResumeId())));
            }
        });
        return LayuiPageFactory.createPageInfo(page);
    }

    @Override
    public LayuiPageInfo findPageBySpec2(BrowseParam param) {
        Page pageContext = getPageContext();
        IPage<BrowseResult> page = this.baseMapper.customPageList(pageContext, param);
        page.getRecords().forEach(item -> {
            if (item.getType()==0){//求职者看职位
                item.setResume(resumeMapper.findBySpec(new ResumeParam().setMemberId(item.getMemberId())));
                //求职者信息
                MemberResult memberResult=memberMapper.getMemberById(item.getMemberId());
                item.setMemberAvatar(memberResult.getAvatar());
                item.setMemberName(memberResult.getName());
                item.setMemberPostName(memberResult.getPostName());
                item.setMemberSex(memberResult.getSex());
                //工作经历
                List<WorkExpResult> list=workExpMapper.customList(new WorkExpParam().setMemberId(item.getMemberId()));
                if (list.size()>0){
                    item.setCompanyName(list.get(0).getCompanyName());
                    item.setCompanyPostName(list.get(0).getPostName());
                    item.setWorkSkill(list.get(0).getSkill());
                }
                //期望薪资
                List<JobExpectResult> expectResults =jobExpectMapper.customList(new JobExpectParam().setId(item.getMemberId()));
                if (expectResults.size()>0){
                    item.setExpectSalary(expectResults.get(0).getExpectSalary());
                }
                //应聘岗位名称
                CompanyPositionResult companyPositionResult =companyPositionMapper.findBySpec(new CompanyPositionParam().setId(item.getPositionId()));
                if (companyPositionResult!=null){
                    item.setPostName(companyPositionResult.getPostName());
                }
            }else if (item.getType()==1){//招聘者看简历
                item.setCompany(companyMapper.findBySpec(new CompanyParam().setId(item.getCompanyId())));
            }
        });
        return LayuiPageFactory.createPageInfo(page);
    }

    @Override
    public LayuiPageInfo findPageBySpecForAdmin(BrowseParam param) {
        Page pageContext = getPageContext();
        IPage<BrowseResult> page =this.baseMapper.customPageListForAdmin(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    private Serializable getKey(BrowseParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private Browse getOldEntity(BrowseParam param) {
        return this.getById(getKey(param));
    }

    private Browse getEntity(BrowseParam param) {
        Browse entity = new Browse();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
